package car.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import dhu.com.car.common.R;
import dhu.com.car.dto.OrderDto;
import dhu.com.car.entity.Orders;
import dhu.com.car.entity.User;
import dhu.com.car.mapper.OrdersMapper;
import dhu.com.car.service.OrdersService;
import dhu.com.car.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService orderService;
    @Autowired
    private UserService userService;

    @Autowired
    private OrdersMapper ordersMapper;


    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Integer id, @DateTimeFormat String beginTime, @DateTimeFormat String endTime){
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        Page<OrderDto> dtoPage = new Page<>();

        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件

        if(id!=null){
            queryWrapper.eq(Orders::getId,id);
        }
        if(beginTime!=null){
            queryWrapper.ge(Orders::getTime,beginTime);
        }
        if(endTime!=null){
            queryWrapper.le(Orders::getTime,endTime);
        }

        //执行查询
        orderService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Orders> records = pageInfo.getRecords();
        List<OrderDto> list=records.stream().map((item)->{
            OrderDto orderDto = new OrderDto();

            BeanUtils.copyProperties(item,orderDto);

            User user = userService.getById(item.getUId());

            orderDto.setName(user.getName());
            orderDto.setPhone(user.getPhone());

            return orderDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);


        return R.success(dtoPage);
    }

    @GetMapping("/{id}")
    public R<Map<String, Double>> getOrdersById(@PathVariable("id") Integer id){

        Map<String, Double> map = new HashMap<String, Double>();

        Double price= Double.valueOf(0);
        List<Orders> orders = ordersMapper.selectOrdersBycid(id);
        for(Orders order:orders){
            price+=order.getPrice();
        }
        map.put("price",price);
        return R.success(map);
    }

    @GetMapping("/user/{id}")
    public R<Page> getUserOrders(@PathVariable("id") Integer id){
        Page<Orders> pageInfo = new Page();
        Page<OrderDto> dtoPage = new Page();
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUId,id);
        orderService.page(pageInfo,queryWrapper);
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Orders> records = pageInfo.getRecords();
        List<OrderDto> list=records.stream().map((item)->{
            OrderDto orderDto = new OrderDto();

            BeanUtils.copyProperties(item,orderDto);

            User user = userService.getById(item.getUId());

            orderDto.setName(user.getName());
            orderDto.setPhone(user.getPhone());

            return orderDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }
}
